<?php 
    $idnam = $_GET['idnam'];
    $idkhoi = $_GET['idkhoi'];
    $idlop = $_GET['idlop'];
    $id_mon = $_GET['idmon'];
    $idlop = $_GET['idlop'];
    $idhs = $_GET['idhs'];
    $templ = new XTemplate('sites/tpml/qldiem/edit.html');
    $sql = "SELECT hocsinh.id AS id_hocsinh, hocsinh.name AS tenhocsinh,hocsinh.mahs AS mahs , 
            khoi.ten AS tenkhoi, lop.ten AS tenlop, namhoc.ten AS tennamhoc, mark . * , monhoc.ten AS tenmonhoc
            FROM hocsinh
            LEFT JOIN lop ON hocsinh.lop = lop.id
            LEFT JOIN khoi ON khoi.id = lop.id_khoi
            LEFT JOIN namhoc ON khoi.id_namhoc = namhoc.id
            LEFT JOIN mark ON mark.hocsinh_id = hocsinh.id
            
            LEFT JOIN monhoc ON monhoc.id = mark.monhoc_id
            WHERE lop.id = $idnam
            AND khoi.id = $idkhoi
            AND namhoc.id = $idlop
            AND mark.monhoc_id=$id_mon AND hocsinh.id=$idhs
        ";
    $data = $db->GetRow($sql);
    //print_r($data);die('xxx');
    
    $diemmieng =json_decode($data['diemmieng']);
    
    $diem15ph =json_decode($data['diem15ph']);
    $diem1t = json_decode($data['diem1t']);
    $diemhk = json_decode($data['diemhk']);
    for($i=0;$i<count($diemmieng);$i++){
        $dmg .= '<input name="diemmieng[]" value="'.$diemmieng[$i].'"><br>';
    }
    for($j=0;$j<count($diem15ph);$j++){
         $d15ph .= '<input name="diem15ph[]" value="'.$diem15ph[$j].'"><br>';
    }
    for($k=0;$k<count($diem1t);$k++){
        $d1t .= '<input name="diem1t[]" value="'.$diem1t[$k].'"><br>';
    }
    for($m=0;$m<count($diemhk);$m++){
        $dhk .= '<input name="diemhk[]" value="'.$diemhk[$m].'"><br>';
    }
   // print_r($diemmieng);die;
    $templ->assign('postidhs',$idhs);
    $templ->assign('postidmon',$id_mon);
    $templ->assign('diem15ph',$d15ph);
    $templ->assign('diem1t',$d1t);
    $templ->assign('diemhk',$dhk);
    $templ->assign('tenmonhoc',$data['tenmonhoc']);
    $templ->assign('diemmieng',$dmg);
    $templ->assign('ten',''.$data['tenhocsinh'].'');
    $templ->assign('mahs',''.$data['mahs'].'');
    $templ->assign('khoi',''.$data['tenkhoi'].'');
    $templ->assign('lop',''.$data['tenlop'].'');
    
    if($_POST){
        $idhs = $_POST['idhs'];
        $idmon = $_POST['idmon'];
        $diemmieng = json_encode($_POST['diemmieng']);
        $diem15ph = json_encode($_POST['diem15ph']);
        $diem1t = json_encode($_POST['diem1t']);
        $diemhk = json_encode($_POST['diemhk']);
        $strs = "UPDATE mark 
                SET diemmieng='$diemmieng', diem15ph='$diem15ph', diem1t='$diem1t', diemhk='$diemhk'
                WHERE hocsinh_id=$idhs AND monhoc_id=$idmon
                ";
       // print_r($strs);die;
        $db->Execute($strs);
        header('Location: index.php');
    }
    $templ->parse('DIEMEDIT');
    $maincontent = $templ->text('DIEMEDIT');
?>